import 'dart:ui';

import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:zrjz/comm/base/base_stateful_page.dart';
import 'package:zrjz/comm/constants/constants.dart';
import 'package:zrjz/comm/utils/ext_dart.dart';
import 'package:zrjz/comm/widget/common_widget.dart';
import 'package:zrjz/comm/widget/my_button.dart';
import 'package:zrjz/comm/widget/my_text_view.dart';
import 'package:zrjz/local/theme/theme_config.dart';
import 'package:zrjz/router/page_router.dart';

class DarkModelPage extends BaseStatefulPage {
  DarkModelPage({super.key});

  //启动当前页面
  static void startInstance() {
    return Get.start(RouterPath.DARK_MODEL);
  }

  @override
  State<DarkModelPage> createState() => _DarkModelPageState();

  @override
  GetxController createRawController() {
    throw UnimplementedError();
  }
}

class _DarkModelPageState extends State<DarkModelPage> {
  int selectedMode = 0; // 0-跟随系统，1-亮色模式，2-暗色模式

  @override
  void initState() {
    super.initState();
    int? model = SpUtil.getInt(AppConstant.storagedarkmodel, defValue: 0);
    setState(() {
      selectedMode = model ?? 0;
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: DarkThemeUtil.multiColors(ColorConstants.pageBg, darkColor: ColorConstants.darkBlackBg),
        appBar: CommonWidget.appBar(context, "深色模式".tr),
        body: SafeArea(
          bottom: true,
          top: false,
          //真正的 Content 布局
          child: Scrollbar(
            child: SingleChildScrollView(
              child: Column(
                mainAxisSize: MainAxisSize.max,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  ListTile(
                    title: MyTextView(
                      '跟随系统',
                      textColor: DarkThemeUtil.multiColors(ColorConstants.black, darkColor: ColorConstants.white),
                      fontSize: 16,
                      isFontMedium: true,
                    ),
                    trailing: CupertinoSwitch(
                      value: selectedMode == 0,
                      onChanged: (value) {
                        setState(() {
                          selectedMode = value ? 0 : 1;
                        });
                      },
                    ),
                  ),
                  Divider(
                    color: DarkThemeUtil.multiColors(ColorConstants.dividerD9, darkColor: ColorConstants.darkBlackItemDivider),
                    height: 0.5,
                  ),
                  if (selectedMode != 0) ...[
                    MyTextView(
                      '手动选择:',
                      textColor: DarkThemeUtil.multiColors(ColorConstants.black, darkColor: ColorConstants.white),
                      fontSize: 16,
                      marginTop: 10,
                      marginBottom: 10,
                      marginLeft: 15,
                      isFontMedium: true,
                    ),
                    ListTile(
                      title: MyTextView(
                        '亮色模式',
                        textColor: DarkThemeUtil.multiColors(ColorConstants.black, darkColor: ColorConstants.white),
                        fontSize: 16,
                        isFontMedium: true,
                      ),
                      trailing: CupertinoSwitch(
                        value: selectedMode == 1,
                        onChanged: (value) {
                          setState(() {
                            selectedMode = 1;
                          });
                        },
                      ),
                    ),
                    Divider(
                      color: DarkThemeUtil.multiColors(ColorConstants.dividerD9, darkColor: ColorConstants.darkBlackItemDivider),
                      height: 0.5,
                    ),
                    ListTile(
                      title: MyTextView(
                        '深色模式',
                        textColor: DarkThemeUtil.multiColors(ColorConstants.black, darkColor: ColorConstants.white),
                        fontSize: 16,
                        isFontMedium: true,
                      ),
                      trailing: CupertinoSwitch(
                        value: selectedMode == 2,
                        onChanged: (value) {
                          setState(() {
                            selectedMode = 2;
                          });
                        },
                      ),
                    ),
                  ],
                  MyButton(
                    text: "应 用".tr,
                    textColor: ColorConstants.white,
                    fontSize: 17,
                    fontWeight: FontWeight.w700,
                    backgroundColor: ColorConstants.appBlue,
                    minHeight: 45,
                    radius: 5,
                    onPressed: () {
                      switch (selectedMode) {
                        case 1:
                          SpUtil.putInt(AppConstant.storagedarkmodel, 1);
                          ThemeConfig.changeThemeLight();
                          break;
                        case 2:
                          SpUtil.putInt(AppConstant.storagedarkmodel, 2);
                          ThemeConfig.changeThemeDark();
                          break;
                        default:
                          SpUtil.putInt(AppConstant.storagedarkmodel, 0);
                          Brightness currentBrightness = MediaQuery.of(context).platformBrightness;
                          if (currentBrightness == Brightness.dark) {
                            ThemeConfig.changeThemeDark();
                          } else {
                            ThemeConfig.changeThemeLight();
                          }
                          break;
                      }
                    },
                  ).marginOnly(left: 28.5, right: 28.5, top: 48),
                ],
              ),
            ),
          ),
        ));
  }
}
